﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using SMS.Controls.SupplierSVC;
using SMS.Common.Silverlight;
using SMS.Entities;
using SMS.Entities.BaseClasses;

namespace SMS.Controls
{
    public partial class SupplierControl : UserControl
    {
        ServiceProxy<SupplierServiceClient> proxy;
        public SupplierControl()
        {
            InitializeComponent();
            if (!DesignerProperties.IsInDesignTool)
            {
                proxy = new ServiceProxy<SupplierServiceClient>("Service/SupplierService.svc");
                proxy.Client.SearchSupplierCompleted += new EventHandler<SearchSupplierCompletedEventArgs>(Client_SearchSupplierCompleted);
                sMSAutoComplete1.SearchTextChanged += new Action<string>(sMSAutoComplete1_SearchTextChanged);
                sMSAutoComplete1.SelectionChanged += new Action<object, object>(sMSAutoComplete1_SelectionChanged);
                sMSAutoComplete1.TextChanged += new Action<string>(sMSAutoComplete1_TextChanged);
                sMSAutoComplete1.SetFilter(SearchSupplier);
                sMSAutoComplete1.SetDataTemplate(this.Resources["SupplierTemplate"] as DataTemplate);
                sMSAutoComplete1.SetValuePath("Supplier_Name");
            }
        }

        void sMSAutoComplete1_TextChanged(string obj)
        {
            SearchText = obj;
        }

        private bool IsNewSearchTextRequired(string searchText)
        {
            return currentSearchText.ToLower() != searchText.ToLower();
        }
        private string currentSearchText = string.Empty;

        void sMSAutoComplete1_SelectionChanged(object arg1, object arg2)
        {

            SelectedSupplier = arg2 as Supplier;
        }

        void sMSAutoComplete1_SearchTextChanged(string obj)
        {
            if (IsNewSearchTextRequired(obj))
            {
                currentSearchText = sMSAutoComplete1.autoCompleteBox1.Text;
                VisualStateManager.GoToState(sMSAutoComplete1, "BusyState", true);
                proxy.Client.SearchSupplierAsync(string.Empty, obj);
            }
            SearchText = obj;
        }

        void Client_SearchSupplierCompleted(object sender, SearchSupplierCompletedEventArgs e)
        {
            sMSAutoComplete1.ItemSource = e.Result as IEnumerable<BaseEntity>;
            VisualStateManager.GoToState(sMSAutoComplete1, "Normal", true);
        }

        public bool SearchSupplier(string search, object value)
        {
            return (value as Supplier).Supplier_ID.ToString().Contains(search.ToLower()) ||
                (value as Supplier).Supplier_Name.ToLower().Contains(search.ToLower());
            //return true;
        }



        public Supplier SelectedSupplier
        {
            get
            {
                return (Supplier)GetValue(SelectedSupplierProperty);
            }
            set
            {
                SetValue(SelectedSupplierProperty, value);
                sMSAutoComplete1.SetSelectedItem(value);
            }
        }

        // Using a DependencyProperty as the backing store for SelectedSupplier.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedSupplierProperty =
            DependencyProperty.Register("SelectedSupplier", typeof(Supplier), typeof(SupplierControl), new PropertyMetadata(null, new PropertyChangedCallback(SelectedSupplierChanged)));



        public static void SelectedSupplierChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var control = sender as SupplierControl;
            control.SelectedSupplier = e.NewValue as Supplier;
        }

        public string SearchText
        {
            get { return (string)GetValue(SearchTextProperty); }
            set { SetValue(SearchTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SearchText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SearchTextProperty =
            DependencyProperty.Register("SearchText", typeof(string), typeof(SupplierControl), new PropertyMetadata(string.Empty, new PropertyChangedCallback(SearchTextPropertyChanged)));

        private static void SearchTextPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var control = sender as SupplierControl;
            control.SearchText = e.NewValue as string;
        }
    }
}
